package org.sundy.action;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UserAction extends ActionSupport {

	private Logger log = Logger.getLogger(UserAction.class.getName());
	private Integer id;

	private String name;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		StringBuffer info = new StringBuffer();
		InputStream in = request.getInputStream();
		BufferedInputStream buf = new BufferedInputStream(in);
		byte[] buffer = new byte[1024];
		int iRead;
		while ((iRead = buf.read(buffer)) != -1) {
			info.append(new String(buffer, 0, iRead, "UTF-8"));
		}
		log.info("the info was " + info.toString());
		JSONObject jsonObj = null;
		if (null != info.toString() || !info.toString().contains("\"")) {
			jsonObj = JSONObject.fromObject(info.toString());
		}
		String appid = jsonObj.getString("appid");
		id = jsonObj.getInt("id");
		name = jsonObj.getString("name");
		log.info("get appid is " + appid);
		response.addHeader("Access-Control-Allow-Origin", "*");
		String hid = request.getHeader("appKey");
		log.info("get appKey " + hid) ;
		//response.addHeader("Access-Control-Allow-Origin", "*");
		return Action.SUCCESS;

	}
}
